#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    //1.创建管道
    int fd[2] = {0};
    if(pipe(fd) < 0)
    {
        perror("pipe");
        return 1;
    }
    //2.创建子进程，并且子进程一直写1字节，然后每次打印count
    pid_t pid = fork();
    if(pid == 0)
    {
        //子进程
        char a = 'a';
        //关闭读端
        close(fd[0]);
        int count = 0;
        while(1)
        {
            count++;
            write(fd[1],&a,1);
            printf("%d\n",count);
        }
        //关闭写端
        close(fd[1]);
        exit(0);
    }
    //3.父进程
    close(fd[1]);//关闭写端
    waitpid(pid,NULL,0);
    close(fd[0]);
    return 0;
}